package cj.web.admin.controllers;

import java.io.File;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import cj.utils.Constants;
import cj.utils.DateUtils;
import cj.utils.ImageUtils;
import cj.utils.ReadConfigProperties;
import cj.utils.ReturnUUID;
import cj.web.admin.dao.ColumnsDAO;
import cj.web.admin.dao.ColumnsRefDAO;
import cj.web.admin.dao.RoleInfoDAO;
import cj.web.admin.dao.SubjectDAO;
import cj.web.admin.domain.Columns;
import cj.web.admin.domain.Subject;
import cj.web.constants.ColumnsStatusEnum;
import cj.web.constants.ColumnsSubTypeEnum;
import cj.web.constants.ColumnsTypeEnum;
import cj.web.constants.MemuEnum;
import cj.web.login.controllers.BaseController;

@Path("/subjectRefCol")
public class SubjectRefColController extends BaseController{

	private static Logger logger = Logger.getLogger(SubjectController.class.getName());
	
	@Autowired
	private ColumnsDAO columnsDao;
	@Autowired
	private RoleInfoDAO roleInfoDao;
	@Autowired
	private ColumnsRefDAO columnsRefDao;
	@Autowired
	private SubjectDAO subjectDao;
	
	@Get("show")
	public String show(Model model,Columns columns) {
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkgztgl);
        model.add("ColumnsStatusEnum",EnumSet.allOf(ColumnsStatusEnum.class));
		model.add("ColumnsSubTypeEnum",EnumSet.allOf(ColumnsSubTypeEnum.class));
		List<Columns> objs = columnsDao.queryAll();
		model.add("Columns",objs);
		return "/views/news/subjectList.vm";
	}

	@Post("getList")
	public String queryList(Subject subject) {
	
		List<Subject> arr = subjectDao.query(subject);
		
		for(Subject obj:arr)
		{
			obj.setCreateAt(DateUtils.getMinusTime(obj.getCreateAt(), DateUtils.DATE_FULL_STR, 0));
			obj.setUpdateAt(DateUtils.getMinusTime(obj.getUpdateAt(), DateUtils.DATE_FULL_STR, 0));
		}

		JSONObject jo = new JSONObject();

		int size = subjectDao.querySize(subject);

		jo.put("data", arr);
		jo.put("draw", subject.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@"+jo.toString();	
	}
	
	@Get("createPage")
	public String createPage(Model model)
	{
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkgztgl);
		model.add("ColumnsSubTypeEnum",EnumSet.allOf(ColumnsSubTypeEnum.class));
		List<Columns> objs = columnsDao.queryAll();
		model.add("Columns",objs);
		return "/views/news/subjectDetail.vm";
	}
	
	@Post("create")
	public String create(Model model,Subject subject,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			subject.setStatus(ColumnsStatusEnum.FB.getValue());
			subject.setCreateUser(returnSessionObj(request).getAccount());
			subject.setUpdateUser(returnSessionObj(request).getAccount());
			subjectDao.create(subject);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Post("upload")
	public String upload(@Param("uploadimage") MultipartFile file,HttpServletRequest request) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		// 判断文件是否为空  
        if (!file.isEmpty()) {  
            try {  
                // 文件保存路径  
            	String path = ReadConfigProperties.getValue("uploadFilePath");
            	//判断文件是否存在，不存在创建文件
            	Map rep = ReturnUUID.getFolder(path);
            	path = rep.get("allpath").toString();//所在的全路径
            	//文件名
                String fileName =  ReturnUUID.getName(file.getOriginalFilename()); 
                // 转存文件  
                file.transferTo(new File(path+"/"+fileName));  
                
                jo.put("filePath", rep.get("filepath")+"/"+fileName);
                
                ImageUtils.scale3(path+"/"+fileName, path+"/y_"+fileName, 460, 640);// 按比例压缩,做适当截取
                
                ImageUtils.scale3(path+"/"+fileName, path+"/l_"+fileName, 432, 750);// 按比例压缩,做适当截取
                
                ImageUtils.scale2(path+"/l_"+fileName, path+"/s_"+fileName, 432, 750, true);//缩率图		750*432
                
            } catch (Exception e) {  
                e.printStackTrace();  
                jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
            }  
        } 
		
		return "@"+jo.toString();
    }
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id)
	{
		Subject obj = subjectDao.queryById(id);
		
		if(obj!=null){
			model.add("obj",obj);
		}
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkgztgl);
		model.add("ColumnsSubTypeEnum",EnumSet.allOf(ColumnsSubTypeEnum.class));
		List<Columns> objs = columnsDao.queryAll();
		model.add("Columns",objs);
		return "/views/news/subjectEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,Subject subject,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			subject.setUpdateUser(returnSessionObj(request).getAccount());
			subjectDao.update(subject);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("id") int id)
	{
		Subject obj = subjectDao.queryById(id);
		
		if(obj!=null)
		{
			subjectDao.deleteById(id);
		}
		return "r:show";
	}
	
	//发布
	@Get("updateByStatusFb")
    public String updateByStatusFb(@Param("id") int id) {
		
		Subject obj = subjectDao.queryById(id);
		
		if(obj!=null)
		{
			subjectDao.updateByStatusFb(id);
		}
		
		return "r:show";
	}
	
	//未发布
	@Get("updateByStatusWfb")
    public String updateByStatusWfb(@Param("id") int id) {
		
		Subject obj = subjectDao.queryById(id);
		
		if(obj!=null)
		{
			subjectDao.updateByStatusWfb(id);
		}
		
		return "r:show";
	}
}
